/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package master_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Renaud & François & Karen
 *
 * Protocol coté serveur,
 *
 */
public class Protocol {

    private Socket s;
    // ServerSocket serveur;
    // private String ip;
    private BufferedReader reception;
    private PrintWriter envoi;

    public Protocol(Socket sock) throws IOException {
        s = sock;
        reception = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println(" connect new reception");
        envoi = new PrintWriter(s.getOutputStream(), true);
        System.out.println("nouveau  new envoie");
        System.out.println("nouveau socket");

    }

    public void stopTCP() throws IOException {  // stop du socket 
        s.close();
        System.out.println("Socket fermé");
    }

    public void StopIN() throws IOException {
        reception.close();
    }

    // --- reader  + Writers 
    public String RedOneString() throws IOException {
        String out = reception.readLine();
        return out;
    }

    public int RedOneInt() throws IOException {
        int out = reception.read();
        return out;
    }

    public void writeOneString(String in) {
        envoi.println(in);
        envoi.flush();
        System.out.println("envoie String " + in);
    }

    public void writeOneInt(int in) {
        envoi.write(in);
        envoi.flush();
        System.out.println("envoie INT " + in);
    }

    public void writeOK() {
        envoi.println("OK");
        System.out.println("envoie OK");
        envoi.flush();
    }

    public void writeNOK() {
        envoi.println("NOK");
        System.out.println("envoie NOK");
        envoi.flush();
    }

    // --- LES GET  &SET
    public Socket getSocket() {
        return s;
    }

    public int getLocalPort() {
        int out = s.getLocalPort();
        return out;
    }

    public int getDistPort() {
        int out = s.getPort();
        return out;
    }

    public String getHostAdressClient() {
        String out = s.getInetAddress().getHostAddress();
        return out;
    }

    public String getHostNameClient() {
        String out = s.getInetAddress().getHostAddress();
        return out;
    }
// envoie des top  10 d'une config
    void sendStats(String[] outStats) {
        writeOneInt(outStats.length);  // on indique le nombre des elements à envoyer  si il ya moins de 10 ou un jour on prends  top 20
        for (int i= 0; i < outStats.length; i++) {
        writeOneString(outStats[i]);
        }
    }
// envoie des stats d'un joueur...
    void sendUStats(String[] outStats) {
        writeOneInt(outStats.length);
        for (int i= 0; i < outStats.length; i++) {
        writeOneString(outStats[i]);
        }
    }
}
